local extension = Package("FkWzry")

Fk:loadTranslationTable{
  ["FkWzry"] = "王者荣耀",
}

-- 技能贪狼
local Wzry_Tanlang = fk.CreateTriggerSkill{
  name = "Wzry_Tanlang",
  anim_type = "offensive",
  frequency = Skill.NotFrequent,
  events = {fk.TargetSpecified},
  -- mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target == player and data.card and (data.card.trueName == "slash" or data.card.name == "duel") and (not player.room:getPlayerById(data.to):isKongcheng())
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      -- room:broadcastSkillInvoke("Wzry_Tanlang")
      -- room:notifySkillInvoked(player, "Wzry_Tanlang")
      local card_id = room:askForCardChosen(room:getPlayerById(data.to), room:getPlayerById(data.to), "h", "Wzry_Tanlang")
      if card_id then
        room:obtainCard(player.id, card_id, true)
        local card = Fk:getCardById(card_id)
        if card.color == Card.Red and player:isWounded() then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = "Wzry_Tanlang"
          })
        elseif card.color == Card.Black then
          room:drawCards(player, 2, self.name)
        end
        -- 第一个true表示可以弃装备，第二个false表示必须弃置
        room:askForDiscard(player, 2, 2, true, self.name, false)
        data.card.skillName = "Wzry_Tanlang"
      end
    end
  end,
}
local Wzry_Tanlang_LoseHp = fk.CreateTriggerSkill{
  name = "#Wzry_Tanlang_LoseHp",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.PreDamage},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("Wzry_Tanlang") and target == player and data.card and (data.card.trueName == "slash" or data.card.name == "duel") and data.card.skillName == "Wzry_Tanlang"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreDamage then
      local room = player.room
      room:loseHp(data.to, data.damage, self.name)
      return true
    end
  end,
}
Wzry_Tanlang:addRelatedSkill(Wzry_Tanlang_LoseHp)
-- 技能饕餮
local Wzry_Taotie_Distance = fk.CreateDistanceSkill{
  name = "#Wzry_Taotie_Distance",
  correct_func = function(self, from, to)
    if from:hasSkill(self.name) then
      return -1
    end
  end,
}
local Wzry_Taotie = fk.CreateTriggerSkill{
  name = "Wzry_Taotie",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.HpLost},
  global = true,
  -- mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- room:broadcastSkillInvoke("Wzry_Taotie")
    -- room:notifySkillInvoked(player, "Wzry_Taotie")
    room:drawCards(player, 1, self.name)
  end,
}
Wzry_Taotie:addRelatedSkill(Wzry_Taotie_Distance)
-- 武将吕布
local Wzry_lvbu = General:new(extension, "Wzry_lvbu", "qun", 4, 4, General.Male)
Wzry_lvbu:addSkill(Wzry_Tanlang)
Wzry_lvbu:addSkill(Wzry_Taotie)
-- 吕布翻译
Fk:loadTranslationTable {
  ["Wzry_lvbu"] = "吕布",
  ["Wzry_Tanlang"] = "贪狼",
  [":Wzry_Tanlang"] = "当你使用[杀]或[决斗]指定目标后，你可以令其交给你一张手牌。若此牌为：1.红色，你回复一点体力 2.黑色，你摸两张牌。若如此做，你需弃置两张牌，然后你令此[杀]造成的伤害视为体力流失。",
  ["$Wzry_Tanlang"] = "从此刻开始，战场由我一人主宰！",
  ["Wzry_Taotie"] = "饕餮",
  [":Wzry_Taotie"] = "锁定技，你计算与其他角色的距离始终-1。每当一名角色失去体力后，你摸一张牌。",
  ["$Wzry_Taotie"] = "心中的黑暗，在不断膨胀！",
  ["~Wzry_lvbu"] = "不要……再回到黑暗中！",
}

-- 技能破云
local Wzry_Poyun = fk.CreateTriggerSkill{
  name = "Wzry_Poyun",
  anim_type = "offensive",
  events = {fk.AfterCardUseDeclared, fk.CardResponding},
  frequency = Skill.NotFrequent,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target == player and data.card.name == "jink"
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- local targets = table.filter(room:getOtherPlayers(player), function(p)
    --   -- TODO: 判断这个目标可不可以被玩家使用【杀】
    --   return p
    -- end)
    
    local targets = {}
    local other = room:getOtherPlayers(player)
    for _, p in ipairs(other) do
      -- TODO: 判断这个目标可不可以被玩家使用【杀】
      table.insert(targets, p.id)
    end
   
    local p = room:askForChoosePlayers(player, targets, 1, 1, "Wzry_Poyun_Tip", self.name)
    if #p > 0 then
      self.cost_data = p[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(self.cost_data)
    local slash = Fk:cloneCard("slash")
    room:useCard{
      from = player.id,
      tos = { { tar.id } },
      card = slash,
    }
    room:drawCards(player, 1, self.name)
  end,
}
-- 技能龙鸣
local Wzry_Longming = fk.CreateTriggerSkill{
  name = "Wzry_Longming",
  anim_type = "defensive",
  events = { fk.DamageInflicted },
  frequency = Skill.NotFrequent,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and #player:getCardIds({Player.Hand, Player.Equip}) >= player.hp and player.hp <= 2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:askForDiscard(player, player.hp, player.hp, true, self.name, false)
    data.damage = data.damage - 1
  end,
}
-- 武将赵云
local Wzry_zhaoyun = General:new(extension, "Wzry_zhaoyun", "shu", 4, 4, General.Male)
Wzry_zhaoyun:addSkill(Wzry_Poyun)
Wzry_zhaoyun:addSkill(Wzry_Longming)
-- 赵云翻译
Fk:loadTranslationTable {
  ["Wzry_zhaoyun"] = "赵云",
  ["Wzry_Poyun"] = "破云",
  [":Wzry_Poyun"] = "每当你使用或打出一张[闪]时，你可以视为对一名其他角色使用一张[杀]（无距离限制）。若如此做，你摸一张牌。",
  ["$Wzry_Poyun"] = "枪尖在燃烧！",
  ["Wzry_Longming"] = "龙鸣",
  [":Wzry_Longming"] = "当你受到伤害时，若你的体力值不大于2，你可以弃置x张牌，令此伤害-1（x为你当前的体力值）。",
  ["$Wzry_Longming"] = "龙枪乱舞，万夫莫敌！",
  ["Wzry_Poyun_Tip"] = "你可以选择一名其他角色视为对其使用一张无限距离的[杀]",
  ["~Wzry_zhaoyun"] = "拜托……各位了……",
}

-- 技能决断（半成品）
-- TODO: 1.拼点需等待新版本；2.标记持续期间内，目标角色不能使用或打出锦囊牌。
local Wzry_Jueduan = fk.CreateActiveSkill{
  name = "Wzry_Jueduan",
  anim_type = "control",
  target_num = 1,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local target = room:getPlayerById(use.tos[1])
    -- TODO: 拼点的代码
    local pindian = player:pindian({ target }, self.name)
		if pindian.results[target.id].winner == player then
			room:setPlayerMark(target, "@@Wzry_Jueduan_Mark", 1)
		end
  end,
}
local Wzry_Jueduan_LoseMark = fk.CreateTriggerSkill{
  name = "#Wzry_Jueduan_LoseMark",
  -- 要使用 refresh，否则当同时机有其他技能时，系统会询问发动哪个技能
  refresh_events = { fk.EventPhaseStart, fk.Death },
  can_refresh = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self.name)) then return false end
    if event == fk.Death then
      return data.who == player
    else
      return player.phase == Player.Start
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local other = room:getOtherPlayers(player)
    for _, p in ipairs(other) do
      room:setPlayerMark(p, "@@Wzry_Jueduan_Mark", 0)
    end
  end,
}
Wzry_Jueduan:addRelatedSkill(Wzry_Jueduan_LoseMark)
-- 技能宿命
local Wzry_Suming = fk.CreateActiveSkill{
  name = "Wzry_Suming",
  anim_type = "support",
  target_num = 1,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local target = room:getPlayerById(use.tos[1])
    local choiceList = {}
		table.insert(choiceList, "Wzry_Suming_Throw_Equip")
		table.insert(choiceList, "Wzry_Suming_Throw_All")
		local choice = room:askForChoice(target, choiceList, self.name)
    if choice == "Wzry_Suming_Throw_Equip" then
      player:throwAllCards("e")
      target:throwAllCards("e")
      if player:isWounded() then
        room:recover{
          who = player,
          num = 1,
          skillName = self.name
        }
      end
      if target:isWounded() then
        room:recover{
          who = target,
          num = 1,
          skillName = self.name
        }
      end
    elseif choice == "Wzry_Suming_Throw_All" then
      player:throwAllCards("he")
      target:throwAllCards("he")
      if player:isWounded() then
        room:recover{
          who = player,
          num = player:getLostHp(),
          skillName = self.name
        }
      end
      if target:isWounded() then
        room:recover{
          who = target,
          num = target:getLostHp(),
          skillName = self.name
        }
      end
      player:turnOver()
			target:turnOver()
    end
  end,
}
-- 武将大乔
local Wzry_daqiao = General:new(extension, "Wzry_daqiao", "wu", 3, 3, General.Female)
Wzry_daqiao:addSkill(Wzry_Jueduan)
Wzry_daqiao:addSkill(Wzry_Suming)
-- 大乔翻译
Fk:loadTranslationTable {
  ["Wzry_daqiao"] = "大乔",
  ["Wzry_Jueduan"] = "决断",
  [":Wzry_Jueduan"] = "出牌阶段限一次，你可以与一名其他角色拼点。若你赢，你令其不能使用或打出锦囊牌直到你的下一个回合开始阶段。（PS：此技能暂时有BUG，即使拼点赢了对方依然可以使用或打出锦囊牌）",
  ["$Wzry_Jueduan"] = "没有方向的河流，终会枯竭。",
  ["@@Wzry_Jueduan_Mark"] = "决断",
  ["Wzry_Suming"] = "宿命",
  [":Wzry_Suming"] = "出牌阶段限一次，你可以令一名其他角色选择一项，然后你与其共同执行：1.弃置装备区里的所有牌并回复一点体力 2.弃置所有牌，将体力值回复至体力上限，再将武将牌翻面。",
  ["$Wzry_Suming"] = "完美，是最无情的禁锢。",
  ["Wzry_Suming_Throw_Equip"] = "弃置装备区里的牌并回复1点体力",
  ["Wzry_Suming_Throw_All"] = "弃置所有牌并回复至体力上限然后翻面",
  ["~Wzry_daqiao"] = "会原谅……我的任性吗？",
}

-- 技能忘忧
local Wzry_Wangyou = fk.CreateActiveSkill{
  name = "Wzry_Wangyou",
  anim_type = "support",
  target_num = 1,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local target = room:getPlayerById(use.tos[1])
    local card_ids = room:getNCards(1)
		local card_id = card_ids[1]
    room:obtainCard(target.id, card_id, true)
    local card = Fk:getCardById(card_id)
    if card.color == Card.Red and target:isWounded() then
      room:recover({
        who = target,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    end
  end,
}
-- 技能无邪
local Wzry_Wuxie = fk.CreateActiveSkill{
  name = "Wzry_Wuxie",
  anim_type = "support",
  target_num = 1,
  card_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == Card.Red
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local target = room:getPlayerById(use.tos[1])
    local card = use.cards[1]
    -- 目前这个方法无法实现控顶，只能控底
    room:moveCardTo(card, Card.DrawPile, player, fk.ReasonPut, self.name)
    if target:isWounded() then
      room:recover({
        who = target,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    end
  end,
}
-- 武将蔡文姬
local Wzry_caiwenji = General:new(extension, "Wzry_caiwenji", "wei", 3, 3, General.Female)
Wzry_caiwenji:addSkill(Wzry_Wangyou)
Wzry_caiwenji:addSkill(Wzry_Wuxie)
-- 蔡文姬翻译
Fk:loadTranslationTable {
  ["Wzry_caiwenji"] = "蔡文姬",
	["Wzry_Wangyou"] = "忘忧",
	[":Wzry_Wangyou"] = "出牌阶段限一次，你可以指定一名其他角色，令其展示牌堆顶的一张牌并获得之。若此牌为红色，你令其回复一点体力。",
	["$Wzry_Wangyou"] = "人家演奏，打架的加油！",
	["Wzry_Wuxie"] = "无邪",
	[":Wzry_Wuxie"] = "出牌阶段限一次，你可以将一张红色手牌置于牌堆顶，然后令一名角色回复一点体力。（PS：此技能目前有BUG，放的牌在牌底而不是牌顶，因此两个技能暂时无法联动）",
	["$Wzry_Wuxie"] = "不要欺负我，会把你弄哭的哟！",
	["~Wzry_caiwenji"] = "呜呜呜，少女心，破碎了……",
}

-- 技能绽放
local Wzry_Zhanfang = fk.CreateTriggerSkill{
  name = "Wzry_Zhanfang",
  anim_type = "drawcard",
  events = { fk.CardUseFinished },
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(self.name) and
      player.phase == Player.Play and
      player:usedSkillTimes(self.name) < 1 and
      ((data.card.type == Card.TypeTrick and data.card.sub_type ~= Card.SubtypeDelayedTrick) or data.card.type == Card.TypeBasic) and
      player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:obtainCard(player.id, data.card, true, fk.ReasonPrey)
  end,
}
-- 技能缭乱
local Wzry_Liaoluan = fk.CreateActiveSkill{
  name = "Wzry_Liaoluan",
  anim_type = "offensive",
  min_target_num = 1,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected < Self:getHandcardNum() and to_select ~= Self.id and Self:inMyAttackRange(Fk:currentRoom():getPlayerById(to_select))
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local num = player:getHandcardNum()
		player:throwAllCards("h")
		for _, target_id in ipairs(use.tos) do
      local target = room:getPlayerById(target_id)
			local response_jink = nil
			response_jink = room:askForResponse(target, "jink", "jink", "Wzry_Liaoluan_Tip:"..player.id..player.id, true)
			if response_jink == nil then
				room:damage({
          from = player,
          to = target,
          damage = 1,
          skillName = self.name,
        })
			else
        room:responseCard({
          from = target_id,
          card = response_jink,
        })
				room:drawCards(player, 1, self.name)
			end
		end
  end,
}
-- 武将小乔
local Wzry_xiaoqiao = General:new(extension, "Wzry_xiaoqiao", "wu", 3, 3, General.Female)
Wzry_xiaoqiao:addSkill(Wzry_Zhanfang)
Wzry_xiaoqiao:addSkill(Wzry_Liaoluan)
-- 小乔翻译
Fk:loadTranslationTable {
  ["Wzry_xiaoqiao"] = "小乔",
  ["Wzry_Zhanfang"] = "绽放",
	[":Wzry_Zhanfang"] = "出牌阶段限一次，当你使用的基本牌或普通锦囊牌结算结束后，你可以获得此牌。",
	["$Wzry_Zhanfang"] = "想看我变魔术吗？别眨眼哦！",
	["Wzry_Liaoluan"] = "缭乱",
	[":Wzry_Liaoluan"] = "出牌阶段限一次，你可以弃置所有手牌，然后指定你攻击范围内的最多X名其他角色（X为你弃置的手牌数）。这些角色需依次选择一项：1.打出一张[闪]并令你摸一张牌 2.受到你造成的一点伤害。",
	["$WzryLiaoluan"] = "流星，回应少女的祈愿吧！",
	["Wzry_Liaoluan_Tip"] = "请弃置一张【闪】并令%src摸一张牌，否则受到%src造成的一点伤害。",
	["~Wzry_xiaoqiao"] = "周瑜大人……",
}

return { extension }